﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Generic;
using System.Data;
using Spring.Data.Common;

namespace HotelApp
{
    public static class AdminInfoDAO
    {
        #region static field

        private static AdoTemplate template = AppConfigurer.AdoTemplate;
        private static Object lockObj = new Object();
        private static string strBase = "select User_Code, User_Name, Password, Phone, Email, Create_Date"
            + " from Admin_Info";

        #endregion

        #region internal methods

        internal static void CreateAdminInfo(AdminInfo obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("AdminInfoDAO.CreateAdminInfo: Invalid parameter");

            lock (lockObj)
            {
                //Generate ItemCode
                string strSearch = "SELECT isnull(max(User_Code),'U000000000') FROM Admin_Info ";
                string strMaxNo = template.ExecuteScalar(CommandType.Text, strSearch).ToString();
                int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
                obj.UserCode = "U" + nNum.ToString("000000000");

                //Insert into table Admin_Info
                string strCreate = "insert into Admin_Info values(@User_Code, @User_Name, @Password,"
                + "@Phone, @Email, @Create_Date)";

                IDbParameters para = template.CreateDbParameters();
                para.Add("User_Code", DbType.String).Value = obj.UserCode;
                para.Add("User_Name", DbType.String).Value = obj.UserName;
                para.Add("Password", DbType.String).Value = obj.Password;
                para.Add("Phone", DbType.String).Value = obj.Phone == null ? string.Empty : obj.Phone;
                para.Add("Email", DbType.String).Value = obj.Email == null ? string.Empty : obj.Email;
                para.Add("Create_Date", DbType.DateTime).Value = obj.CreateDate;

                template.ExecuteNonQuery(CommandType.Text, strCreate, para);
            }
        }

        internal static void UpdateAdminInfo(AdminInfo obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("AdminInfoDAO.UpdateAdminInfo: Invalid parameter");

            string strSql = "update Admin_Info set User_Name=@User_Name, Password=@Password,"
                + " Phone=@Phone, Email=@Email where User_Code=@User_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("User_Code", DbType.String).Value = obj.UserCode;
            para.Add("User_Name", DbType.String).Value = obj.UserName;
            para.Add("Password", DbType.String).Value = obj.Password;
            para.Add("Phone", DbType.String).Value = obj.Phone == null ? string.Empty : obj.Phone;
            para.Add("Email", DbType.String).Value = obj.Email == null ? string.Empty : obj.Email;

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static void DeleteAdminInfo(string userCode)
        {
            string strSql = "delete from Admin_Info where User_Code=@User_Code";
            IDbParameters para = template.CreateDbParameters();
            para.Add("User_Code", DbType.String).Value = userCode;

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static AdminInfo LoginSystem(string uesrName, string password)
        {
            string strSql = strBase + " where User_Name=@User_Name and Password=@Password";

            IDbParameters para = template.CreateDbParameters();
            para.Add("User_Name", DbType.String).Value = uesrName;
            para.Add("Password", DbType.String).Value = password;

            IList<AdminInfo> lst = template.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<AdminInfo>(MapRow), para);

            return lst.Count > 0 ? lst[0] : null;
        }

        #endregion

        #region public methods

        public static AdminInfo GetAdminInfo(string userCode)
        {
            string strSql = strBase + " where User_Code=@User_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("User_Code", DbType.String).Value = userCode;

            IList<AdminInfo> lst = template.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<AdminInfo>(MapRow), para);

            return lst.Count > 0 ? lst[0] : null;
        }

        public static IList<AdminInfo> ListAdminInfos()
        {
            return template.QueryWithRowMapperDelegate(CommandType.Text, strBase,
                new RowMapperDelegate<AdminInfo>(MapRow));
        }

        #endregion

        #region private methods

        private static AdminInfo MapRow(IDataReader dr, int rownumber)
        {
            AdminInfo obj = new AdminInfo();
            obj.UserCode = dr.IsDBNull(0) ? "" : dr.GetString(0);
            obj.UserName = dr.IsDBNull(1) ? "" : dr.GetString(1);
            obj.Password = dr.IsDBNull(2) ? "" : dr.GetString(2);
            obj.Phone = dr.IsDBNull(3) ? "" : dr.GetString(3);
            obj.Email = dr.IsDBNull(4) ? "" : dr.GetString(4);
            obj.CreateDate = dr.IsDBNull(5) ? DateTime.MinValue : dr.GetDateTime(5);

            return obj;
        }

        #endregion
    }
}
